<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of account
 *
 * @author lz06
 */
class ItemTypeDb {

    private $dbc;

    function __construct(DBC $dbc) {
        $this->dbc = $dbc;
    }

    /**
     * 获得商品列表
     * @param type $type
     * @param type $start
     * @param type $limit
     * @return type
     */
    public function getItemType($start = 0, $limit = 1000) {
        if (!is_numeric($start)) {
            $start = 0;
        }
        if (!is_numeric($limit)) {
            $limit = 10;
        }
        $sql = "SELECT TOP " . $limit . " * FROM ItemType WHERE id_x NOT IN(SELECT TOP " . $start . " id_x FROM ItemType);";
        return $this->dbc->fetchData($sql);
    }

    /**
     * 根据商品类型ID获得商品类型信息
     * @param type $id
     * @return type
     */
    public function getItemTypeById($id) {
        $sql = "SELECT * FROM ItemType WHERE id_x = " . $id;
        return $this->dbc->fetchData($sql);
    }

    /**
     * 更新名称
     * @param type $itemTypeName
     * @param type $id
     * @return type
     */
    public function modifyItemType($itemTypeName, $id) {
        $sql = "UPDATE ItemType SET item_type = '" . $itemTypeName . "' WHERE id_x = " . $id;
        return $this->dbc->execute($sql);
    }

    /**
     * 删除类型
     * @param type $idx
     * @return type
     */
    public function deleteItemType($idx) {
        $sqls = array();
        $sql = "UPDATE ItemInfo SET type = 0 where type = " . $idx;
        array_push($sqls, $sql);
        $sql = "DELETE from ItemType  WHERE id_x = " . $idx;
        array_push($sqls, $sql);
        return $this->dbc->executeBatch($sqls);
    }

     /**
     * 增加商品类型
     * @param type $itemTypeName
     * @param type $id
     * @return type
     */
    public function createItemType($itemTypeName) {
        $sql = "INSERT INTO ItemType(item_type) VALUES('" . $itemTypeName . "')";
        return $this->dbc->execute($sql);
    }
}
